package com.mccken.annotation.logoperator.context;

import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.ParameterNameDiscoverer;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-01-04 18:10
 **/
public class LogRecordEvaluationContext extends MethodBasedEvaluationContext {
	
	public LogRecordEvaluationContext(Object rootObject, Method method, Object[] arguments, ParameterNameDiscoverer parameterNameDiscoverer, Object ret, String errorMsg) {
		
		// 把方法的参数都放到SpEL解析的RootObject中
		super(rootObject, method, arguments, parameterNameDiscoverer);
		
		// 把LogRecordContext 中的变量都放到RootObject中
		Map<String, Object> variables = LogRecordContext.getVariables();
		if (variables != null && variables.size() > 0) {
			for (Map.Entry<String,Object> entry : variables.entrySet()) {
				setVariable(entry.getKey(), entry.getValue());
			}
		}
		
		// 把方法的返回值和ErrorMsg，都放到 RootObject 中
		setVariable("_ret", ret);
		setVariable("_errorMsg", errorMsg);
	}
	
	
}
